import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class SubPrePost{
    String preOrder;
    String postOrder;
    public SubPrePost(String preOrder,String postOrder) {
        this.preOrder=preOrder;
        this.postOrder=postOrder;
    }
}
//Pre And Post(前序和后序)
public class demo02 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        while (sc.hasNext()){
            int m=sc.nextInt();
            if(m==0){
                break;
            }
            String preOrder=sc.next();
            String postOrder=sc.next();

            System.out.println(CalcProssible(m,preOrder,postOrder));
        }
    }
    public static long CalcProssible(int m,String preOrder,String postOrder){
        if(preOrder.isEmpty()||preOrder.length()==1){
            return 1;
        }

        //分离出根的子树
        List<SubPrePost> subTree=splitSubTree(preOrder,postOrder);
        long treeCount=calcCom(m,subTree.size());

        //递归在对子树求解
        for (SubPrePost e:subTree) {
            treeCount*=CalcProssible(m,e.preOrder,e.postOrder);
        }

        return treeCount;
    }
    public static List<SubPrePost> splitSubTree(String pre,String post){
        List<SubPrePost> subTree=new ArrayList<>();
        int preIdx=1;
        int postFirst=0;
        while (preIdx<pre.length()){
            char subRoot=pre.charAt(preIdx);
            int postRootIdx=post.indexOf(subRoot);
            int nodeCount=postRootIdx-postFirst+1;
            subTree.add(new SubPrePost(pre.substring(preIdx,preIdx+nodeCount),
                    post.substring(postFirst,postFirst+nodeCount)));
            preIdx+=nodeCount;
            postFirst+=nodeCount;
        }
        return subTree;
    }

    public static long calcCom(int n,int m){
        m=Math.min(m,n-m);
        long r=1;
        for (int i = n-m+1; i <= n; i++) {
            r*=i;
        }
        return r/fac(m);
    }

    public static long fac(int m){
        long r=1;
        for (int i = 1; i <= m; i++) {
            r*=i;
        }
        return r;
    }
}
